package zj.com.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.zfdang.multiple_images_selector.ImagesSelectorActivity;
import com.zfdang.multiple_images_selector.SelectorSettings;

import org.xutils.image.ImageOptions;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import zj.com.activities.JcdDetailEditActivity;
import zj.com.activities.LookImgActivity;
import zj.com.net.Urls;
import zj.com.util.ImageUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class UploadImgGridAdapter extends BaseAdapter{

    public List<String> list;
    public Activity context;
    public LayoutInflater inflater;

    public boolean isEdit = false;
    public DisplayImageOptions options;

    public UploadImgGridAdapter(List<String> list, Activity context){

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
        options = ImageUtil.getOption();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder ;
        if (convertView == null) {
            viewHolder   = new ViewHolder();
            convertView = inflater.inflate(R.layout.adapter_upload_img_grid, null);
            viewHolder.img_iv = (ImageView) convertView.findViewById(R.id.img_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if(position == list.size() -1){
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.add_img_icon,viewHolder.img_iv);
        }else{
            Uri uri;
               String str = list.get(position);
            if(str.startsWith("http")){
                String index = str.replace(Urls.GetResourceContent, "");
                str = String.format(Urls.GetImg, index, "Min");
            }else{
                 uri= Uri.fromFile(new File(str));

                str = uri.toString();

            }

            ImageSize mImageSize = new ImageSize(50, 50);
            ImageLoader.getInstance().displayImage(str,viewHolder.img_iv,mImageSize);
        }
            viewHolder.img_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(list.get(position).startsWith("http")){
                        List<String> imgs = new ArrayList<String>();
                        for(String img:list){

                            if(img.startsWith("http")){
                                imgs.add(img);
                            }
                        }
                        Intent intent =new Intent(context,LookImgActivity.class);
                        intent.putExtra("position",position);
                        intent.putExtra("imgs",(Serializable)imgs);
                        context.startActivity(intent);
                    }else{

                        Intent intent = new Intent(context, ImagesSelectorActivity.class);
                        // max number of images to be selected
                        intent.putExtra(SelectorSettings.SELECTOR_MAX_IMAGE_NUMBER, 10);

                        // min size of image which will be shown; to filter tiny images (mainly icons)
                        intent.putExtra(SelectorSettings.SELECTOR_MIN_IMAGE_SIZE, 100000);
                        // show camera or not
                        intent.putExtra(SelectorSettings.SELECTOR_SHOW_CAMERA, true);
                        // pass current selected images as the initial value
                        intent.putStringArrayListExtra(SelectorSettings.SELECTOR_INITIAL_SELECTED_LIST, JcdDetailEditActivity.mResults);
                        // start the selector
                        context.startActivityForResult(intent, JcdDetailEditActivity.REQUEST_CODE);
                    }



                }
            });



        return convertView;
    }
    private static class ViewHolder {
        ImageView img_iv;

    }

    public void setDataChange(List<String> list){


        this.list = list;
        notifyDataSetChanged();
    }

}
